<template>
  <div>
    <a-form-item :label="currentItem.defaultValue.label">

    <t-cascader :key="key"
                v-model="currentItem.defaultValue.value"
                :load-data="loadData"
                v-bind="propAttrs"
    ></t-cascader>
    </a-form-item>
  </div>
</template>

<script>
import IndexMixin from "../mixins/index"
import request from "@/utils/request"
export default {
  name: "gen-cascader",
  mixins:[IndexMixin],
  methods:{
    loadData(data,callback){
      if(data.first){
        request.request({
          url:this.currentItem.__field_props__["first-url"].value,
          method:this.currentItem.__field_props__["first-method"].value
        }).then(res=>{
          const fields = this.currentItem.__field_props__["first-response-key"].value.split(".")
          const data = fields.reduce((current,key)=>{
              try {
                return current[key]
              }catch (e){
                return {}
              }
          },res)
          callback(data)
        })
      }else{
        request.request({
          url:this.currentItem.__field_props__["other-url"].value,
          method:this.currentItem.__field_props__["other-method"].value
        }).then(res=>{
          const fields = this.currentItem.__field_props__["response-key"].value.split(".")
          const data = fields.reduce((current,key)=>{
            try {
              return current[key]
            }catch (e){
              return {}
            }
          },res)
          callback(data)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
